<?php defined('_JEXEC') or die('Restricted access');
include_once JPATH_ROOT . "/libraries/joomla/html/html/select.php";
JHTML::_('behavior.formvalidation');

require_once JPATH_ROOT . "/administrator/components/com_jbschedule/media/js/xajax/xajax_core/xajax.inc.php";
$document =& JFactory::getDocument();
$document->addScript(JURI::base().'/components/com_jbschedule/media/js/301a.js' );

$xajax = new xajax();
$xajax->printJavascript("components/com_jbschedule/media/js/xajax/");

?>
<script type="text/javascript">
document.write("<div id=\"tsbLoading\"></div>");
xajax.callback.global.onRequest = function() {xajax.$('tsbLoading').style.display = 'block';}
xajax.callback.global.beforeResponseProcessing = function() {xajax.$('tsbLoading').style.display='none';}
function updateAssetGroups(){
	//Set Ajax URI
	xajax.config.requestURI = "http://localhost/jbschedule/administrator/index.php?option=com_jbschedule&controller=assetgroups&task=ajaxGetAll";
	xajax.config.waitCursor = true;
	//Call the Server side Callback function located in the action specified by above URI
	xajax.call('updateAssetGroups');
}

function myValidate(form) {
	var error = '';
	var ok = true;
	for(i=0; i<form.elements.length; i++){
		if (! document.formvalidator.validate(form.elements[i])) {
			if( ok ) ok = false;

			switch(form.elements[i].name){
				case 'name':
						error += 'Name is required!\n';
					break;
			}
		}
	}
	if(error != '')
		alert(error);

	if(ok)
		form.check.value='<?php echo JUtility::getToken(); ?>';//send token

	return ok;
}

//Override the base function in folder: include/js/joomla.javascript.js
function submitbutton(pressbutton) {
	if(pressbutton.toUpperCase() != 'CANCEL'){
		if(myValidate(document.adminForm))
			submitform(pressbutton);
	}else{
		submitform(pressbutton)
	}
}

</script>
<!--
Chu y: onsubmit="return false" de tranh viec submit form khi go Enter tai mot textbox.
Viec Submit se van dc thuc hien khi bam nut Save tren Toolbar thong qua co che Override ham onsubmit
 -->
<form action="index.php" method="post" name="adminForm" id="adminForm" class="form-validate" onsubmit="return false">
<input type="hidden" name="check" value="post"/>

<div class="col100">
	<fieldset class="adminform">
		<legend><?php echo JText::_( 'Details' ); ?></legend>
		<table class="admintable">
		<tr>
			<td width="100" align="right" class="key">
				<label for="assetgroup_id">
					<?php echo JText::_( 'Group' ); ?>:
				</label>
			</td>
			<td>
			<select class="text_area" name="assetgroup_id" id="assetgroup_id" class="required">
			<?php
			foreach ($this->assetgroups as $group) {
				echo "<option value=\"".$group->id."\" ".($group->id === $this->assets->assetgroup_id ? " selected " : "") . ">".$group->name."</option>";
			}
			?>
			</select>

			<input type="button" value="Show group" onclick="updateAssetGroups();" />
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="name">
					<?php echo JText::_( 'Name' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area required" type="text" name="name" id="name" size="32" maxlength="250" value="<?php echo $this->assets->name;?>" />
				<span class="require_mark">*</span>
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="code">
					<?php echo JText::_( 'ASSET CODE' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="code" id="code" size="6" maxlength="6" value="<?php echo $this->assets->code;?>" />
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="color">
					<?php echo JText::_( 'COLOR' ); ?>:
				</label>
			</td>
			<td>
				<div id="colorpicker301" class="colorpicker301"></div>
				<input type="text" id="color" name="color" size="9" value="<?php echo $this->assets->color;?>">&nbsp;
				<input type="text" size="1" id="valueColor" style="background: <?=$this->assets->color?>">
				<input type="button" onclick="showColorGrid3('color','valueColor');" value="...">

			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="img">
					<?php echo JText::_( 'Image' ); ?>:
				</label>
			</td>
			<td>
				<?php
				//Get All files in a folder (Joomla 1.0)
				//$imgFiles 	= mosReadDirectory( $mosConfig_absolute_path . '../images/tsb' );

				//Get All files in a folder (Joomla 1.5)
				$imgFiles 	= JFolder::files('../images/tsb', '', true, false);

//				$images 	= array( mosHTML::makeOption( '', 'Select image' ) );

				$images 	= array( JHTMLSelect::option( '', 'Select image' ) );

				foreach( $imgFiles AS $file ) {
					if( eregi( 'bmp|gif|jpg|jpeg|png', $file )) {
						$images[] = JHTMLSelect::option( $file );
					}
				}

				$imagelist = JHTMLSelect::genericlist ($images, 'img', "class=\"inputbox\" size=\"1\""
				. " onchange=\"javascript:if (document.forms[0].img.options[selectedIndex].value!='') {".
				"document.imagelib.src='../images/tsb/' + document.forms[0].img.options[selectedIndex].value} else {document.imagelib.src='../images/M_images/blank.png'}\"",
				'value', 'text', $this->assets->img );
				//if(strlen(trim($this->assets->img) > 0)){
				?>
				<p>
				<img border="0" alt="Preview" name="imagelib" src="../images/tsb/<?=$this->assets->img?>" style="border: 1px solid #ccc; padding: 6px;"/>
				</p>
				<?
				echo '<p>'.$imagelist.'</p>';
				//}
				?>
				<img src="components/com_jbschedule/media/images/48/web_uploads.png" />&nbsp;<b><a href="index.php?option=com_media" onclick="alert('Images must be placed in \'tsb\' folder')">Upload images</a></b>
				<div style="color: red; display: inline; padding-left: 15px">* Note: Images must be placed in <b>tsb</b> folder</div>
			</td>
		</tr>
		<tr>
			<td width="100" align="right" class="key">
				<label for="description">
					<?php echo JText::_( 'Description' ); ?>:
				</label>
			</td>
			<td>
				<textarea class="text_area" name="description" id="description" rows="4" cols="60" maxlength="250"><?php echo $this->assets->description;?></textarea>
			</td>
		</tr>
	</table>
	</fieldset>
</div>
<div class="clr"></div>
<input type="hidden" name="option" value="com_jbschedule" />
<input type="hidden" name="id" value="<?php echo $this->assets->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="assets" />
</form>
